﻿namespace KingerRobot.MexicoSailunMDC.SpiService;

[Logging]
public class Wms2MesImpl : IWms2MesService
{
    /// <inheritdoc/>
    public int PushOutOrderStatus(List<PushOutOrderStatusParam> pushOutOrderStatusParams)
    {
        var result = 0;
        try
        {
            List<PushOutOrderStatusMesParam> pushOutOrderStatusMesParams = new();
            foreach (var pushOutOrderStatusParam in pushOutOrderStatusParams)
            {
                pushOutOrderStatusMesParams.Add(new()
                {
                    OrderDetailId = pushOutOrderStatusParam.OrderDetailId,
                    OrderStatus = pushOutOrderStatusParam.OrderStatus,
                    TireBarcode = pushOutOrderStatusParam.TireBarcode
                });
            }
            var mesService = K8.RSF.Client.Create<IMesWebService>();
            result = mesService.PushOutOrderStatus(pushOutOrderStatusMesParams);
           
        }
        catch (Exception ex)
        {
            WriteLog($"8.出库订单状态（WMS->MES）出库订单发生变化时,推送出库订单状态 异常：{ex.Message}", true);
        }
        return result;
    }
    /// <inheritdoc/>
    public int PushWarehousing(string tireBarcode, string currentLoc)
    {
        var result = 0;
        try
        {
            var mesService = K8.RSF.Client.Create<IMesWebService>();
            result = mesService.PushWarehousing(tireBarcode, currentLoc);
        }
        catch (Exception ex)
        {
            WriteLog($"9.入库信息（WMS->MES）胎胚扫码后，进行胎胚入库，由WMS将信息推送至MES 异常：{ex.Message}", true);
        }
        return result;
    }
    /// <inheritdoc/>
    public int PushWarehousingOut(List<PushWarehousingOutParam> pushWarehousingOutParams)
    {
        var result = 0;
        try
        {
            List<PushWarehousingOutMesParam> pushWarehousingOutMesParams = new();
            foreach (var pushWarehousingOutParam in pushWarehousingOutParams)
            {
                pushWarehousingOutMesParams.Add(new()
                {
                    OrderDetailId = pushWarehousingOutParam.OrderDetailId,
                    TireBarcode = pushWarehousingOutParam.TireBarcode,
                    CurrentLoc = pushWarehousingOutParam.CurrentLoc,
                    TargetLoc = pushWarehousingOutParam.TargetLoc,
                    EquipNo = pushWarehousingOutParam.EquipNo,
                    EquipModel = pushWarehousingOutParam.EquipModel
                });
            }
            var mesService = K8.RSF.Client.Create<IMesWebService>();
            result = mesService.PushWarehousingOut(pushWarehousingOutMesParams);
        }
        catch (Exception ex)
        {
            WriteLog($"10.出库信息（WMS->MES）开始执行堆垛机出库任务，由WMS将信息推送至MES 异常：{ex.Message}", true);
        }
        return result;
    }
    /// <inheritdoc/>
    public int PushDirectDelivery(PushDirectDeliveryParam pushDirectDeliveryParam)
    {
        var result = 0;
        try
        {
            var mesService = K8.RSF.Client.Create<IMesWebService>();
            result = mesService.PushDirectDelivery(new()
            {
                BarCode = pushDirectDeliveryParam.BarCode,
                OrderDetailId = pushDirectDeliveryParam.OrderDetailId.ToString(),
                CurrentLoc = pushDirectDeliveryParam.CurrentLoc.ToString(),
                EquipNo = pushDirectDeliveryParam.EquipNo,
                EquipModel = pushDirectDeliveryParam.EquipModel.ToString(),
            });
        }
        catch (Exception ex)
        {
            WriteLog($"11.直送信息（WMS->MES） 异常：{ex.Message}", true);
        }
        return result;
    }
    public int PushWarehousingFinish(string tireBarcode, string currentLoc)
    {
        var result = 0;
        try
        {
            var mesService = K8.RSF.Client.Create<IMesWebService>();
            result = mesService.PushWarehousingFinish(tireBarcode, currentLoc);
        }
        catch (Exception ex)
        {
            WriteLog($"12.入库完成（WMS->MES）堆垛机入库任务完成后，由WMS将信息推送至MES 异常：{ex.Message}", true);
        }
        return result;
    }
    public int PushDirectDeliveryFinish(PushDirectDeliveryFinishParam pushDirectDeliveryFinishParam)
    {
        var result = 0;
        try
        {
            var mesService = K8.RSF.Client.Create<IMesWebService>();
            result = mesService.PushDirectDeliveryFinish(new()
            {
                BarCode = pushDirectDeliveryFinishParam.BarCode,
                OrderDetailId = pushDirectDeliveryFinishParam.OrderDetailId.ToString(),
                EquipNo = pushDirectDeliveryFinishParam.EquipNo,
                EquipModel = pushDirectDeliveryFinishParam.EquipModel,
            });
        }
        catch (Exception ex)
        {
            WriteLog($"13.直送完成（WMS->MES） 异常：{ex.Message}", true);
        }
        return result;
    }
    /// <inheritdoc/>
    public int PushWarehousingOutFinish(List<PushWarehousingOutFinishParam> pushWarehousingOutFinishParams)
    {
        var result = 0;
        try
        {
            List<PushWarehousingOutFinishMesParam> pushWarehousingOutFinishMesParams = new();
            foreach (var pushWarehousingOutFinishParam in pushWarehousingOutFinishParams)
            {
                pushWarehousingOutFinishMesParams.Add(new()
                {
                    OrderDetailId = pushWarehousingOutFinishParam.OrderDetailId,
                    TireBarcode = pushWarehousingOutFinishParam.TireBarcode,
                    TargetLoc = pushWarehousingOutFinishParam.TargetLoc,
                    EquipNo = pushWarehousingOutFinishParam.EquipNo,
                    EquipModel = pushWarehousingOutFinishParam.EquipModel
                });
            }
            var mesService = K8.RSF.Client.Create<IMesWebService>();
            result = mesService.PushWarehousingOutFinish(pushWarehousingOutFinishMesParams);
        }
        catch (Exception ex)
        {
            WriteLog($"14.出库完成（WMS->MES）出库任务EMS放胎完成到达硫化机后，由WMS将信息推送至MES 异常：{ex.Message}", true);
        }
        return result;
    }
    /// <inheritdoc/>
    public int PushAbnormal(string barCode, long startLoc, long targetLoc)
    {
        var result = 0;
        try
        {
            var mesService = K8.RSF.Client.Create<IMesWebService>();
            result = mesService.PushAbnormal(barCode, startLoc, targetLoc);
        }
        catch (Exception ex)
        {
            WriteLog($"16.异常输送（WMS->MES） 异常：{ex.Message}", true);
        }
        return result;
    }

    public ProductBean ProductGreenTire1(string productCode)
    {
        ProductBean productBean = null;
        try
        {
            ProductGreenTireParam? product = null;
            var mesService = K8.RSF.Client.Create<IMesWebService>();
            product = mesService.ProductGreenTire(productCode);
            if (product is null)
            {
                return null;
            }
            productBean = new()
            {
                TireBarcode = product.TireBarcode,
                MaterialId = product.MaterialId,
                ProductEquip = product.ProductEquip,
                ProductTime = product.ProductTime,
                TireLock = product.TireLock,
                QuailtyStatus = product.QuailtyStatus,
                ProductBatch = product.ProductBatch,
            };
        }
        catch (Exception ex)
        {
            WriteLog($"获取产品信息 异常：{ex.Message}", true);
        }

        return productBean;

    }
}
